<?php

namespace App\Admin\Controllers;

use App\TravelPlan;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\DB;

class TravelPlanController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '行程计划列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new TravelPlan());

        $grid->column('id', __('Id'));
        $grid->column('user_id', __('用户id'));
        $grid->column('trip_plan_city', __('途径城市'));
        $grid->column('trip_way', __('出行方式'))->using([1 => '乘车', 2 => '自驾']);
        $grid->column('trip_plan_way', __('意向出行方式'))->using([1 => '公共汽车', 2 => '轮船', 3 => '火车', 4 => '飞机']);
        $grid->column('plan_share_days', __('计划天数'));

        /*$grid->column('price_json', '费用详情')->expand(function ($model) {


            $comments = $model->where('id',$this->id)->get()->map(function ($comment) {
                return $comment->only(['price_json']);
            });
            var_dump($comments);
            die;
            $comment_arr = $comments->toArray();
            $arr = json_decode($comment_arr[0]['price_json'], true);

            $arr2 = [];
            foreach ($arr as $k=>$v){
                $arr2[] = [$v['price_name'],$v['num'],$v['standard'],$v['price'],$v['exchange_rate']];
            }



            return new Table(['用户名', '数量', '标准', '小计', '汇率'], $arr2);
        });*/
        $grid->column('price_json', '费用管理')->expand(function ($model) {
            $comments = DB::table('travel_price')->where('plan_id',$this->id)->get()->map(function ($comment) {
                return $comment;
            });
            return new Table(['id','费用名','数量','标准','小计','汇率'], $comments->toArray());
        });
        $grid->column('attraction_ison', '景点管理')->expand(function ($model) {
            $comments = DB::table('travel_attractions')->where('plan_id',$this->id)->get()->map(function ($comment) {
                return $comment;
            });
            return new Table(['id','景点名','同行人数','金额/人','小计','汇率'], $comments->toArray());
        });
        /*$grid->column('price_json', __('Price json'));
        $grid->column('attraction_ison', __('Attraction ison'));*/
        $grid->column('start_time', __('开始时间'));
        $grid->column('end_time', __('结束时间'));
        $grid->column('start_city', __('开始城市'));
        $grid->column('end_city', __('结束城市'));
        $grid->column('trip_num', __('行程人数'));
        $grid->column('title', __('标题'));

        $grid->column('remark', __('备注'));
        $grid->column('is_lock', __('是否锁定编辑'))->using([1 => '锁定', 0 => '正常']);
        $grid->column('is_publish', '是否发布')->bool(['2' => true, '1' => false]);
        $grid->column('all_price', __('总价'));
        $grid->column('success_status', '是否出行')->bool(['2' => true, '1' => false]);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('修改时间'));

        // 全部关闭
        $grid->disableActions();

        $grid->disableCreateButton();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(TravelPlan::findOrFail($id));
        $de = DB::table('travel_plan')->where('id',$id)->first();
        $de =  objectToArray($de);
        $price_res = $de['price_json'];

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('trip_plan_ids', __('Trip plan ids'));
        $show->field('trip_plan_city', __('Trip plan city'));
        $show->field('trip_way', __('Trip way'));
        $show->field('trip_plan_way', __('Trip plan way'));
        $show->field('image', __('Image'));
        $show->field('plan_share_days', __('Plan share days'));
        $show->field('price_json', __('Price json'));


        $show->field('attraction_ison', __('Attraction ison'));
        $show->field('start_time', __('Start time'));
        $show->field('end_time', __('End time'));
        $show->field('start_city', __('Start city'));
        $show->field('end_city', __('End city'));
        $show->field('trip_num', __('Trip num'));
        $show->field('title', __('Title'));
        $show->field('remark', __('Remark'));
        $show->field('is_lock', __('Is lock'));
        $show->field('is_publish', __('Is publish'));
        $show->field('all_price', __('All price'));
        $show->field('success_status', __('Success status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new TravelPlan());

        $form->number('user_id', __('User id'));
        $form->text('trip_plan_ids', __('Trip plan ids'));
        $form->text('trip_plan_city', __('Trip plan city'));
        $form->number('trip_way', __('Trip way'))->default(1);
        $form->number('trip_plan_way', __('Trip plan way'));
        $form->image('image', __('Image'));
        $form->number('plan_share_days', __('Plan share days'));
        $form->textarea('price_json', __('Price json'));
        $form->textarea('attraction_ison', __('Attraction ison'));
        $form->text('start_time', __('Start time'));
        $form->text('end_time', __('End time'));
        $form->text('start_city', __('Start city'));
        $form->text('end_city', __('End city'));
        $form->number('trip_num', __('Trip num'));
        $form->text('title', __('Title'));
        $form->text('remark', __('Remark'));
        $form->number('is_lock', __('Is lock'))->default(1);
        $form->number('is_publish', __('Is publish'))->default(2);
        $form->decimal('all_price', __('All price'));
        $form->number('success_status', __('Success status'));

        return $form;
    }
}
